<?php

namespace app\stores\controller\admin;

use app\BaseController;
use app\beautiful\model\StaffModel;
use app\common\logic\TokenLogic;
use app\common\model\account\AccountModel;

class Login extends BaseController {

    public function login()

    {

        $mobile=$this->request->param('mobile');

        $shop_password=$this->request->param('shop_password');

        if(empty($mobile)){

            return $this->error('请输入手机号');

        }

        if(empty($shop_password)){

            return $this->error('请输入密码');

        }

        $where=[

            ['mobile','=',$mobile],

            ['shop_password','=',md5($shop_password)]

        ];
        $account = AccountModel::with(['shopAccount'=>function($query){

            $query->where([['role_id','in',[1,2]]]);

        }])->where($where)->find();

        if(null === $account){

            return $this->error('手机号或者密码不正确');

        }

        if($account->is_delete !=0) return $this->error('手机号或者密码不正确');

        if($account->status != 1) return $this->error('您已经离职了');

        if(empty($account->shopAccount)) return $this->error('该账号无权限');

        $auth_token=TokenLogic::getShopToken($account->shopAccount->shop_id,$account->account_id);

        return $this->success(['auth_token'=>$auth_token]);

    }

}

